package com.emagic.manage.modules.room.view;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by Administrator on 2017/3/21.
 */
public class ClassRoomUser {

    public static int PUBLISH_STATE_NONE = 0;
    public static int PUBLISH_STATE_AUDIOONLY = 1;
    public static int PUBLISH_STATE_VIDEOONLY = 2;
    public static int PUBLISH_STATE_BOTH = 3;

    public String nickName = "";
    public String peerId = "";
    public int role = -1;//0 teacher 1 assistant 2 student 3 sideuser 4 hidden
    public boolean hasAudio = false;
    public boolean hasVideo = false;
    public boolean canDraw = false;
    public String giftnumber = String.valueOf(0);

    public boolean raiseHand = false;

    public int publishState = PUBLISH_STATE_NONE;
    public JSONObject properties;

    @Override
    public String toString() {
        return "ClassRoomUser{" +
                "nickName='" + nickName + '\'' +
                ", peerId='" + peerId + '\'' +
                ", role=" + role +
                ", hasAudio=" + hasAudio +
                ", hasVideo=" + hasVideo +
                ", canDraw=" + canDraw +
                ", giftnumber='" + giftnumber + '\'' +
                ", raiseHand=" + raiseHand +
                ", publishState=" + publishState +
                ", properties=" + properties +
                '}';
    }

    ClassRoomUser() {
    }

    ClassRoomUser(Map<String, String> map) {
        peerId = map.get("id");
        String streams = map.get("streams");
        String strproperties = map.get("properties");
        if (strproperties == null || !(strproperties instanceof String) || strproperties.isEmpty())
            return;

        try {
            properties = new JSONObject(strproperties);
            if (properties.has("role"))
                role = properties.optInt("role");
            nickName = properties.optString("nickname");
            hasAudio = getBoolean("hasaudio");
            hasVideo = getBoolean("hasvideo");
            canDraw = getBoolean("candraw");
            publishState = properties.optInt("publishstate");
            raiseHand = getBoolean("raisehand");
            giftnumber = properties.optString("giftnumber");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private boolean getBoolean(String key) {

        try {
            Object tmp = properties.get(key);
            if (tmp instanceof Boolean)
                return (Boolean) tmp;
            else
                return (Integer.valueOf(properties.get(key).toString()) != 0);
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }
}
